local gongqi = fk.CreateSkill{
    name = "SP_heg__gongqi",
}


Fk:loadTranslationTable{
    ["SP_heg__gongqi"] = "弓骑",
    [":SP_heg__gongqi"] = "准备阶段，你可以弃置一张牌并选择一名装备区内有场上攻击范围最大的武器牌的其他角色，你将其的武器牌置入你的装备区；"..
    "结束阶段，若你装备区内有武器牌，你将此牌置入其的装备区内。",

    ["#heg_gongqi-choose"] = "弓骑：弃置一张牌并选择一名装备区里有场上攻击范围最大的武器牌的其他角色，你将其的武器牌置入你的装备区。",

    ["$SP_heg__gongqi1"] = "敌寇首级，且看吾一箭取之！",
    ["$SP_heg__gongqi2"] = "末将尤善骑射，今示于主公一观。",
}


gongqi:addEffect(fk.EventPhaseStart,{
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(gongqi.name) and player.phase == Player.Start
    and not player:isNude() and table.find(player.room:getOtherPlayers(player), function (p)
      return #p:getEquipments(Card.SubtypeWeapon) > 0
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
      local targetss = table.filter(room:getOtherPlayers(player), function (p)
        return #p:getEquipments(Card.SubtypeWeapon) > 0
      end)
      local targets = table.filter(targetss, function (p)
      return table.every(targetss, function (q)
        return p:getAttackRange() >= q:getAttackRange()
      end)
    end)
    if #targets == 0 then return end
    local tos, cards = room:askToChooseCardsAndPlayers(player,{
      min_card_num = 1,
      max_card_num = 1,
      min_num = 1,
      max_num = 1,
      prompt = "#heg_gongqi-choose",
      pattern = ".",
      targets = targets,
      skill_name = gongqi.name,
      cancelable = true,
    })
    if #tos > 0 and #cards > 0 then
      event:setCostData(self, {tos = tos, cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = event:getCostData(self).cards[1]
    local to = event:getCostData(self).tos[1]
    room:throwCard(card,gongqi.name,player,player)
    local weapon = to:getEquipments(Card.SubtypeWeapon)
    if #weapon > 0 then
      local id = weapon[1]
      room:moveCardIntoEquip(player, id, gongqi.name, true, player)
      room:setPlayerMark(player, "SP_heg__gongqi_target-turn", to.id)
    end
  end,
})

gongqi:addEffect(fk.EventPhaseStart, {
  anim_type = "special",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(gongqi.name) and player.phase == Player.Finish
    and player:getMark("SP_heg__gongqi_target-turn") ~= 0 and #player:getEquipments(Card.SubtypeWeapon) > 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local weapon = player:getEquipments(Card.SubtypeWeapon)
    local to = room:getPlayerById(player:getMark("SP_heg__gongqi_target-turn"))
    room:moveCardIntoEquip(to, weapon, gongqi.name, true, player)
  end,
})


return gongqi